package com.example.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

@Configuration
public class DuridConfig {
    @ConfigurationProperties(prefix = "spring.datasource")//解决上一步的属性映射问题
    @Bean
    public DataSource druid(){//重新配置DruidDataSource
        return new DruidDataSource();
    }
    //配置druid的监控
    //1.配置一个管理后台的servlet
    @Bean
    public ServletRegistrationBean statViewBean(){
        ServletRegistrationBean bean=new ServletRegistrationBean(new StatViewServlet(),"/druid/*");
        Map map=new HashMap<>();
        map.put("loginUsername","admin");//设置后台用户名
        map.put("loginPassword","admin");//设置后台密码
        map.put("allow","");//允许所有地址访问
        map.put("pany","192.168.93.90");//禁止某些地址访问
        bean.setInitParameters(map);
        return bean;
    }
    //2.配置一个监控的filter
    @Bean
    public FilterRegistrationBean statFilterBean(){
        FilterRegistrationBean bean=new FilterRegistrationBean();
        bean.setFilter(new WebStatFilter());
        Map map=new HashMap<>();
        map.put("exclusions","*.css,*.js,/druid/*");//排除某些请求
        bean.setInitParameters(map);
        //拦截所有请求
        bean.setUrlPatterns(Arrays.asList("/*"));
        return  bean;
    }

}
